import router from "@/router";

export default {
  state: {
    tagList: [
      // 标签页
      {
        title: "首页",
        path: "/Index",
      },
    ],
  },
  getters: {
    tags({ tagList }) {
      return tagList;
    },
  },
  mutations: {
    // 添加
    newtag({ tagList }, item) {
      console.log(tagList, 99);
      if (item.name == "Kong" || item.name == "Home") {
        return;
      }
      const arr = {
        title: item.meta.title,
        path: item.path,
      };
      for (const e of tagList) {
        // 判断路由里title相等就返回，不在加相同的
        if (e.title == item.meta.title) {
          return;
        }
      }
      if (item.name == "listAdd" || item.name == "listEdite") {
        let index = tagList.findIndex((item) => item.title == "商品列表");
        index = index + 1;
        tagList.splice(index, 0, arr);
        return;
      }
      tagList.push(arr); // 添加
    },

    // 删除
    delTag({ tagList }, index) {
      tagList.splice(index, 1); // 删除数组下标
      let title = router.currentRoute.value.meta.title;
      if (title == "商品添加" || title == "商品修改") {
        router.push("/bcommodity/List");
        return;
      }
      if (index == tagList.length) {
        router.push(tagList[index - 1].path);
      } else router.push(tagList[index].path);
    },
  },

  actions: {
    // 添加
    newtag({ commit }, v) {
      commit("newtag", v);
    },

    // 删除
    delTag({ commit }, v) {
      commit("delTag", v);
    },
  },
};
